package com.rain.generator;

import com.baomidou.mybatisplus.core.toolkit.StringPool;
import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.InjectionConfig;
import com.baomidou.mybatisplus.generator.config.*;
import com.baomidou.mybatisplus.generator.config.po.TableInfo;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;
import com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine;
import com.rain.base.BaseManager;
import com.rain.base.BaseManagerImpl;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.ResourceBundle;

import static com.baomidou.mybatisplus.annotation.DbType.MYSQL;

/**
 * 代码生成类
 * @author: wu
 * @date: 2021/3/18
 */
public class CodeGenerator {
    public static void main(String[] args) {
        // 代码生成器
        AutoGenerator mpg = new AutoGenerator();

        ResourceBundle resource = ResourceBundle.getBundle("application");

        // 全局配置
        GlobalConfig gc = new GlobalConfig();
        String projectPath = System.getProperty("user.dir");
        gc.setOutputDir(projectPath + "/src/main/java");
        gc.setAuthor(resource.getString("author"));
        gc.setOpen(false);
        gc.setFileOverride(true);
        gc.setBaseResultMap(true);
        gc.setBaseColumnList(true);
        gc.setSwagger2(true);
        gc.setEntityName("%sDO");
        gc.setMapperName("%sDAO");
        gc.setServiceName("%sManager");
        gc.setServiceImplName("%sManagerImpl");
        mpg.setGlobalConfig(gc);

        // 数据源配置
        DataSourceConfig dsc = new DataSourceConfig();
        dsc.setUrl(resource.getString("jdbc.url"));
        dsc.setDriverName("com.mysql.cj.jdbc.Driver");
        dsc.setUsername(resource.getString("jdbc.username"));
        dsc.setPassword(resource.getString("jdbc.password"));
        mpg.setDataSource(dsc);

        // 包配置
        PackageConfig pc = new PackageConfig();
        pc.setModuleName(resource.getString("packageName"));
        pc.setEntity("domain");
        pc.setMapper("dao");
        pc.setController("controller");
        pc.setService("manager");
        pc.setServiceImpl("manager.impl");
        pc.setParent("com.rain");
        mpg.setPackageInfo(pc);
        // 策略配置
        StrategyConfig strategy = new StrategyConfig();
        // DAO设置
        strategy.setSuperMapperClass("com.rain.base.BaseDAO");
        // Manager设置
        strategy.setSuperServiceClass("com.rain.base.BaseManager");
        strategy.setSuperServiceImplClass("com.rain.base.BaseManagerImpl");
        // Controller设置
        strategy.setRestControllerStyle(true);
        strategy.setSuperControllerClass("com.rain.base.BaseController");
        // 实体类设置
        strategy.setEntitySerialVersionUID(true);
        strategy.setNaming(NamingStrategy.underline_to_camel);
        strategy.setColumnNaming(NamingStrategy.underline_to_camel);
        strategy.setSuperEntityClass(com.rain.base.BaseDO.class);
        strategy.setEntityLombokModel(true);
        strategy.setEntityBooleanColumnRemoveIsPrefix(true);
        strategy.setEntityTableFieldAnnotationEnable(true);
        strategy.setSuperEntityColumns("id","gmt_create","gmt_modified","is_delete");
        strategy.setInclude(resource.getString("tables").split(","));
        strategy.setControllerMappingHyphenStyle(true);
        String prefix = resource.getString("tabPrefix");
        if (StringUtils.hasLength(prefix)) {
            strategy.setTablePrefix(prefix + "_");
        }
        mpg.setStrategy(strategy);
        mpg.execute();
    }
}
